4

I checked your example, and it's working in your example, but when I tried it in my example, it doesn't work. I don't know where I am making mistakes. Is it that I am using old jQuery files? I am posting the whole HTML code here.

HTML CODE

@{
    Layout = null;
}


<!DOCTYPE html>
<html>
<head>
    <title>Refresh method with new url</title>
    <meta charset="utf-8">
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
    <link href="~/Content/bootstrap-table.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/bootstrap.min.js"></script>
    <script src="~/Scripts/bootstrap-table.js"></script>
</head>
<body>
    <div class="container">
        <div class="ribbon">
            <a href="https://github.com/wenzhixin/bootstrap-table-examples/blob/master/409.html">View Source on GitHub</a>
        </div>
        <h1>Refresh method with new url(<a href="https://github.com/wenzhixin/bootstrap-table/issues/409">#409</a>).</h1>
        <div class="toolbar">
            <button id="refresh" class="btn btn-default">Refresh</button>
        </div>
        <table id="table"
               data-toggle="table"
               data-toolbar=".toolbar"
               data-url='/Report/GetShipments'>
            <thead>
                <tr>
                    <th data-field="id">ID</th>
                    <th data-field="name">Item Name</th>
                    <th data-field="price">Item Price</th>
                </tr>
            </thead>
        </table>
    </div>
    <script>
        var $table = $('#table');
        $(function () {
            $('#refresh').click(function () {
                $table.bootstrapTable('refresh', {
                    url: '/Report/ShowShipment'
                });
            });
        });
    </script>
</body>
</html>

Controller code.

public class ReportController : Controller
    {
        // GET: Report
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetShipments()
        {
            //IShipments shipments = new ShipmentsClient();
            //var data = shipments.GetShipments(string.Empty, string.Empty, string.Empty, string.Empty);
            //return Json(data, JsonRequestBehavior.AllowGet);

            return Json("test" , JsonRequestBehavior.AllowGet);
            //return new Json()
        }

        public ActionResult ShowShipment()
        {
            //IShipments shipments = new ShipmentsClient();
            //var data = shipments.GetShipments(string.Empty, string.Empty, string.Empty, string.Empty);
            //return Json(data, JsonRequestBehavior.AllowGet);

            return Json("test", JsonRequestBehavior.AllowGet);
            //return new Json()
        }

        public ActionResult Test()
        {
            return View();
        }

    }

When the page loads and hits the GetShipment() method in the controller, and then we click the refresh button, it should hit the ShowShipment() method, but unfortunately it always hits the GetShipment() method. I don't know what mistake I am making.

3 Answers 3

17

You can use refresh method:

$table.bootstrapTable('refresh', {
    url: 'new url'
});

I have added a example here: http://issues.wenzhixin.net.cn/bootstrap-table/#issues/409.html

The documentation: http://bootstrap-table.wenzhixin.net.cn/documentation/#methods

Hope to help you.

Sign up to request clarification or add additional context in comments.

3 Comments

I updated code. I am surprised why your example working and mine not.
Finally it works for me. I found the problem that I am using old bottstrap-table.js 1.2 which doesn't support refresh method with new url. Finnally I used bootstrap-table.js version 1.5 and it works for me. Thanks a lot everyone for your effort,
In case of multiple tables on the same page, how to send table name to the url?
4

The best way to set/change data-url of bootstrap table is to use refresh method together with refreshOptions:

$table.bootstrapTable('refreshOptions', {url: 'new url'});
$table.bootstrapTable('refresh', {url: 'new url'});

If you don't do refreshOptions then BootstrapTable still will use Old url for next refreshes. For example for pagination/sorting/search when server-side enabled.

If you need to add loader icon, then you should add before calling refresh method, and delete loader icon with onLoadSuccess, onLoadError events.

Comments

0

I think you're looking for the data function http://api.jquery.com/data/

$('#mytable').data('url', 'yoururl')

Edit :

Well I took a closer look :

  • You have two include of bootstrap-table.css
  • Bootstrap files aren't included as well as jQuery, are they included elsewhere ?
  • Do you get errors in the javascript console ?

Got that working :

<html>
<head>
</head>
<body>
<style type="text/css">
    .the-legend {
        border-style: none;
        border-width: 0;
        font-size: 14px;
        line-height: 20px;
        margin-bottom: 0;
    }

    .the-fieldset {
        border: 2px groove threedface #444;
        -webkit-box-shadow: 0px 0px 0px 0px #000;
        box-shadow: 0px 0px 0px 0px #000;
    }
</style>

<div class='form-horizontal well'>
    <fieldset class="well the-fieldset">
        <legend class="the-legend"><span class="h3">Shipment Filter Criteria</span></legend>
        <div class='row'>
            <div class='col-sm-3'>
                <div class='form-group'>
                    <label for="user_locale">Carrier</label>
                    <select class="form-control" style="width:200px;" id="user_locale" name="user[locale]">
                        <option value="None" selected="selected">Select Carrier Type</option>
                        <option value="UPS">UPS</option>
                        <option value="USPS">USPS</option>
                        <option value="FEDEX">FEDEX</option>
                    </select>
                </div>
            </div>
            <div class='col-sm-3'>
                <div class='form-group'>
                    <label for="user_locale">Package Type</label>
                    <select class="form-control" style="width:200px;" id="user_locale" name="user[locale]">
                        <option value="None" selected="selected">Select Package Type</option>
                        <option value="UPS">UPS</option>
                        <option value="USPS">USPS</option>
                        <option value="FEDEX">FEDEX</option>
                    </select>
                </div>
            </div>
        </div>
        <div class='row'>
            <div class='col-sm-3'>
                <div class='form-group'>
                    <label for="user_locale">Shipped Date</label>
                    <input class="form-control" style="width:200px;" data-provide="datepicker" id="shippedDate" />
                </div>
            </div>
            <div class='col-sm-3'>
                <div class='form-group'>
                    <label for="user_locale">Schedule Date</label>
                    <input class="form-control" style="width:200px;" data-provide="datepicker" id="scheduleDate" />
                </div>
            </div>
            <div class='col-sm-6'>
                <div>
                    <button style="width:150px;height:38px;margin-top:25px;" class="btn btn-primary"
                            onclick="getShipmens()">
                        View Report
                    </button>
                </div>
            </div>
        </div>
    </fieldset>
    <div class="row">
        <br />
        <table id="table-javascript"></table>
        <table id="mytable" data-url='@Url.Action("GetShipments", "Report", new { carrierType = "UPS", shippedDate = "01/12/14" , scheduleDate = "01/12/14" }, null)' data-toggle="table">
            <thead>
                <tr>
                    <th data-field="Id" data-sortable="True">Id</th>
                    <th data-field="TrackingNr" data-sortable="True">Tracking Nr</th>
                    <th data-field="Carrier" data-sortable="True">Carrier</th>
                    <th data-field="PackageType" data-sortable="True">Package Type</th>
                    <th data-field="ShippedDate" data-sortable="True">Shipped Date</th>
                    <th data-field="ScheduledDate" data-sortable="True">Scheduled Date</th>
                    <th data-field="ActivityZip" data-sortable="True">Activity Zip</th>
                    <th data-field="ActivityState" data-sortable="True">Activity State</th>
                </tr>
            </thead>
        </table>
    </div>

    <script src="./Scripts/jquery.min.js"></script>
    <script src="./Scripts/bootstrap-table.min.js"></script>
    <link rel="stylesheet" href="./Content/bootstrap-table.min.css">
    <script src="./Scripts/bootstrap.min.js"></script>
    <link rel="stylesheet" href="./Content/bootstrap.min.css">


    <script type="text/javascript">
    // When the document is ready
    $(document).ready(function () {

        $('#shippedDate').datepicker({
            format: "dd/mm/yyyy"
        });

        $('#scheduleDate').datepicker({
            format: "dd/mm/yyyy"
        });

    });

    function getShipmens() {

        alert($('#mytable').data('url'));

        $('#mytable').data('url', '@Url.Action("GetShipments", "Report")');

        alert($('#mytable').data('url'));

        $('#mytable').bootstrapTable('refresh');
    }

</script>
</div>
</body>
</html>

6 Comments

can you give your HTML please ?
I am using bootstrap table and wants to set data-url attribute using javascript and then wants to refresh table so that it can pull all data from specified url.
It's data('url', 'yoururl') not data('data-url','')
I tried both but when I called refresh method of table then it send old url not modified one.
I up voted you for your effort but it seems that I am not explaining problem well. Have you tried with any new url because it always hits old urls with old values. Whatever I modified it doesn't work. Thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.